﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SocialIn.Biz;
using System.Data;

namespace SocialIn.Web.Members
{
    public partial class InputOptionInfo : System.Web.UI.Page
    {
        protected string PageType { get; set; }
        Join _join;
        string _userid;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                Initialize();
        }

        private void Initialize()
        {
            if (HttpContext.Current.Session[ConfigName.SessionMember] == null)
            {
                AlertMessage("로그인후 이용해 주세요!", "/members/login.aspx");
                return;
            }

            if (Request.QueryString["type"] == "R002")
                PageType = "받으미";
            else
                PageType = "드리미";

            _join = new Join();

            Member userinfo = HttpContext.Current.Session[ConfigName.SessionMember] as Member;

            dlTalentCategory.DataSource = _join.GetMyTalentList(userinfo.UserID);
            dlTalentCategory.DataBind();

            DataTable dt = _join.GetUserInfo(userinfo.UserID);
            txtName.Text = dt.Rows[0]["USERNAME"].ToString();
            txtAddress.Text = dt.Rows[0]["ADDRESS"].ToString();
            txtIntroduction.Text = dt.Rows[0]["SELFINTRODUCTION"].ToString();
            txtPhoneNum.Text = dt.Rows[0]["PHONENUM"].ToString();
            ltrmail.Text = dt.Rows[0]["EMAIL"].ToString();
            imgUserPicture.ImageUrl = userinfo.PicturePath;

        }


        protected void binChangeOK_Click(object sender, EventArgs e)
        {
            string redirectUrl = "/members/InputOptionInfo.aspx";
            if (string.IsNullOrEmpty(txtName.Text))
            {
                AlertMessage("이름을 입력해주세요!", redirectUrl);
                return;
            }

            _join = new Join();
            _userid = (HttpContext.Current.Session[ConfigName.SessionMember] as Member).UserID;

            UserInfo uinfo = new UserInfo()
            {
                 UserName = txtName.Text,
                 Address = txtAddress.Text,
                 SelfIntroduction = txtIntroduction.Text,
                 PhoneNum = txtPhoneNum.Text,
                 UserID = _userid
            };

            List<string> checkedCodeList = new List<string>();

            foreach (DataListItem item in dlTalentCategory.Items)
            {
                CheckBox cbTalent = item.FindControl("cbTalent") as CheckBox;
                if (cbTalent.Checked)
                {
                    HiddenField hidCode = item.FindControl("hidCode") as HiddenField;
                    checkedCodeList.Add(hidCode.Value);
                }
            }

            if (checkedCodeList.Count > 3)
            {
                AlertMessage("재능은 최대 3가지만 선택 가능합니다!", redirectUrl);
                return;
            }

            if (!_join.UpdateUser(uinfo))
                AlertMessage("입력 정보가 올바른지 확인해주세요!", redirectUrl);
            else
                AlertMessage("정보가 변경되었습니다!", redirectUrl);

            _join.UpdateTalent(checkedCodeList, _userid);

        }

        protected void dlTalentCategory_DataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem ||
                e.Item.ItemType == ListItemType.Item)
            {
                DataRow row = ((DataRowView)e.Item.DataItem).Row;

                CheckBox cbTalent = e.Item.FindControl("cbTalent") as CheckBox;
                HiddenField hidCode = e.Item.FindControl("hidCode") as HiddenField;

                cbTalent.Text = row["CodeName"].ToString();
                hidCode.Value = row["SUBCATEGORY"].ToString();

                if (Convert.ToInt32(row["Checked"]) == 1)
                    cbTalent.Checked = true;
                else
                    cbTalent.Checked = false;
            }
        }

        private void AlertMessage(string message, string redirectUrl)
        {
            message = string.Format("<script language=javascript>alert('{0}'); location.href='{1}';</script>", message, redirectUrl);
            ClientScript.RegisterClientScriptBlock(this.GetType(), "join", message);
        }
    }
}